/**
 * 
 */
package com.alstudio.app;

import java.io.File;
import java.io.IOException;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Environment;

import com.alstudio.module.sns.base.SnsConfig;
import com.alstudio.pojo.module.server.ServerInfo;
import com.alstudio.utils.log.ALLog;
import com.nostra13.universalimageloader.utils.L;

/**
 * 应用程序配置文件。可配置应用是国内版还是国际版，登陆方式。服务器配置信息，调试信息
 * 
 * @author alonso lee
 * 
 */
public class VersionConfig {

	// 国内版
	public static final int FOR_CHINA = 1;
	// 国际版
	public static final int FOR_INTERNATIONAL = 2;
	// 应用版本标志位，用于标示国际版还是国内版。缺省为国内版
	public static final int APP_FOR_LOCATION = FOR_CHINA;

	// 图片保存路径
	public static String IMAGE_PATH = "images";
	// 语音保存路径
	public static String AUDIO_PATH = "audio";

	// 更新安装包
	public static String UPDATE_APK_PATH = "apks";
	// 启动界面
	public static String SPLASH_PATH = "splash";

	// 以系统账号登陆
	public static final int LOGIN_TYPE_LOOVEE = 1;
	// 以facebook账号登陆
	public static final int LOGIN_TYPE_FACEBOOK = 2;
	// 以twitter登陆
	public static final int LOGIN_TYPE_TWITTER = 3;
	// 以新浪账号登陆
	public static final int LOGIN_TYPE_SINA = 4;
	// 以qq账号登陆
	public static final int LOGIN_TYPE_TENCENT = 5;

	public static final String LOOVEE_DIR = "imaohu";

	// 应用版本号
	private final String appVersion;

	// 是否为测试版
	private boolean isDebugVersion;
	// 是否输出日志
	private boolean isShowLog = false;
	// 服务器配置文件
	private ServerInfo mServerInfo;
	// 缺省分发器1
	public String disp1;
	// 缺省分发器2
	public String disp2;
	// 分发器端口
	public String port;

	private String missionUrl;

	private String taskJava;

	/**
	 * 
	 */
	public VersionConfig(Context context, boolean isDebugVersion) {
		// TODO Auto-generated constructor stub
		// 获取版本号
		appVersion = getVersion(context);
		this.isDebugVersion = isDebugVersion;
		mServerInfo = new ServerInfo();
		loadServerInfo();
		checkDir(context);
		isShowLog = this.isDebugVersion;
		// 设置日志输出功能
		ALLog.setDebug(isShowLog);
	}

	/**
	 * 获取服务器配置文件
	 */
	public ServerInfo loadServerInfo() {

		if (isDebugVersion) {

			SnsConfig.WEIXIN_APP_ID = SnsConfig.WEIXIN_DEBUG_APP_ID;
			/* 测试分发器地址 */
			disp1 = "116.254.203.66";
			disp2 = "116.254.203.66";

			// 德普
			// port = "11225";
			// 鲍鱼
			port = "16225";

			// 116.254.203.117:11225

			missionUrl = "http://testapi.imaohu.com/index.php";

			taskJava = "http://116.254.203.66:18480/Task";

			// 任务url
			mServerInfo.setMissionUrl("http://testapi.imaohu.com/index.php");

			// 缺省测试服务器地址
			mServerInfo.setIMServerIP("116.254.203.117");
			// 缺省运营服务器端口
			mServerInfo.setIMSocketPort("5555");
			// 域名
			mServerInfo.setServerName("mk");
			// 媒体服务器地址
			mServerInfo.setMediaServerIp("img1.imeach.com");
			// 媒体服务器端口
			mServerInfo.setMediaServerPort("9090");
			// im服务器http 端口
			mServerInfo.setHttpPort("5559");
			// 报纸url
			mServerInfo
					.setNewsUrl("http://116.254.203.70:8080/newspaper/newspaper_list.php");

			mServerInfo.setExtendChargeUrl("http://211.151.60.213:16000");
			mServerInfo.setExtendLbs("http://211.151.60.213:16000");
			mServerInfo.setExtendOther("http://211.151.60.213:16000");

			mServerInfo
					.setChargeCallbackUrl("http://211.151.60.213:16000/IMExtendWebService/servlet/YeePayServlet");
		} else {
			SnsConfig.WEIXIN_APP_ID = SnsConfig.WEIXIN_RELEASE_APP_ID;

			// 国内版,运营分发器
			disp1 = "220.231.193.78";
			disp2 = "116.254.203.73";

			port = "9225";

			missionUrl = "http://api.imaohu.com/index.php";

			// 任务url
			mServerInfo.setMissionUrl("http://api.imaohu.com/index.php");

			taskJava = "http://116.254.203.66:18480/Task";

			// 缺省运营服务器地址
			mServerInfo.setIMServerIP("116.254.203.38");
			// 缺省运营服务器端口
			mServerInfo.setIMSocketPort("5555");
			// 域名
			mServerInfo.setServerName("mk");
			// im服务器http 端口
			mServerInfo.setHttpPort("5559");
			// 媒体服务器地址
			mServerInfo.setMediaServerIp("img1.imeach.com");
			// 媒体服务器端口
			mServerInfo.setMediaServerPort("9090");
			// 报纸url
			mServerInfo
					.setNewsUrl("http://116.254.203.70:8080/newspaper/newspaper_list.php");

			// 经济系统
			mServerInfo.setExtendChargeUrl("http://116.254.203.69:8080");
			// LBS管理
			mServerInfo.setExtendLbs("http://116.254.203.68:28080");
			// 其他
			mServerInfo.setExtendOther("http://116.254.203.68:8080");
			// 易宝充值
			mServerInfo
					.setChargeCallbackUrl("http://116.254.203.69:8080/IMExtendWebService/servlet/YeePayServlet");

		}
		return mServerInfo;

	}

	/**
	 * 获取服务器配置信息
	 * 
	 * @return
	 */
	public ServerInfo getServerInfo() {
		return mServerInfo;
	}

	/**
	 * 设置服务器配置信息
	 * 
	 * @param mServerInfo
	 *            the info to set
	 */
	public void setServerInfo(ServerInfo info) {
		this.mServerInfo = info;
	}

	/**
	 * @return the missionUrl
	 */
	public String getMissionUrl() {
		return missionUrl;
	}

	/**
	 * @return the taskJava
	 */
	public String getTaskJava() {
		return taskJava;
	}

	/**
	 * 获取应用版本号
	 * 
	 * @return the appVersion
	 */
	public String getAppVersion() {
		return appVersion;
	}

	/**
	 * 设置是否为调试版本
	 * 
	 * @param isDebugVersion
	 *            the isDebugVersion to set
	 */
	public void setDebugVersion(boolean isDebugVersion) {
		this.isDebugVersion = isDebugVersion;
	}

	/**
	 * 设置是否输出日志
	 * 
	 * @param isShowLog
	 *            the isShowLog to set
	 */
	public void setShowLog(boolean isShowLog) {
		this.isShowLog = isShowLog;
		ALLog.setDebug(isShowLog);
	}

	/**
	 * 检测是否为调试版本
	 * 
	 * @return the isDebugVersion
	 */
	public boolean isDebugVersion() {
		return isDebugVersion;
	}

	/**
	 * 检测是否输出日志
	 * 
	 * @return the isShowLog
	 */
	public boolean isShowLog() {
		return isShowLog;
	}

	/**
	 * 获取应用版本号
	 * 
	 * @param context
	 * @return
	 */
	private String getVersion(Context context) {
		try {
			PackageInfo pi = context.getPackageManager().getPackageInfo(
					context.getPackageName(), 0);
			return "V" + pi.versionName;
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}
	}

	private void checkDir(Context context) {
		IMAGE_PATH = getExternalCacheDir(context, LOOVEE_DIR, IMAGE_PATH);
		AUDIO_PATH = getExternalCacheDir(context, LOOVEE_DIR, AUDIO_PATH);
		UPDATE_APK_PATH = getExternalCacheDir(context, LOOVEE_DIR,
				UPDATE_APK_PATH);

		SPLASH_PATH = getExternalCacheDir(context, LOOVEE_DIR,
				SPLASH_PATH);

	}

	private String getExternalCacheDir(Context context, String path, String dir) {
		File dataDir = new File(Environment.getExternalStorageDirectory(), path);
		File appCacheDir = new File(dataDir, dir);
		if (!appCacheDir.exists()) {
			if (!appCacheDir.mkdirs()) {
				L.w("Unable to create external cache directory");
				return null;
			}
			try {
				new File(appCacheDir, ".nomedia").createNewFile();
			} catch (IOException e) {
				L.i("Can't create \".nomedia\" file in application external cache directory");
			}
		}
		return appCacheDir.getAbsolutePath();
	}

}
